// pages/index/video_surveillance/video_surveillance.js
import {
  Alert,
  Confirm,
  Toast
} from 'wuss-weapp';
let app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    monitorList: [],
    currentMonitorSrc: "",
    currentMonitorId: "",
    isFullScreen: false,
    address: "",
    tude: "",
    yard_name: "",
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.getCurrentLocationByServer();
    wx.setNavigationBarTitle({
      title: app.globalData.appTitle,
    })
    this.videoLoad();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  },
  /**
   * 獲取视频
   */
  videoLoad: function() {
    app.wxGetRequest("/life/getYardVideo", {
      yard_code: app.globalData.parentAreaCode
    }).then(result => {
      let data = result.data.data;
      let newData=[];
      if (result.data.code == 1) {
        if (data.length > 0) {
          data.forEach((item,index)=>{
            // if (item.monitor_name == "后门" || item.monitor_name == "前门") {
              newData.push(item)
            // }
          });
          this.setData({
            monitorList: newData
          });
        } else {
          this.setData({
            monitorList: []
          });
        }
      }
    });
  },
  /**
   * 一键报警
   */
  alarm: function(e) {
    this.getCurrentLocationByServer();
    let monitorId = e.target.dataset.monitorId;
    if (!monitorId) {
      this.setData({
        error:"请选择报警监控!"
      });
      return;
    }
    wx.showActionSheet({
      itemList: ['急救(120)', '火警(119)','报警(110)'],
      success: res => {
        let type;
        let phone;
         if (res.tapIndex == 1) {
          type = '急救(120)';
          phone="120";
        } else if (res.tapIndex == 2) {
          type = '火警(119)';
          phone="119";
        }else{
          type = '报警(110)';
          phone = "110";
        }
        wx.makePhoneCall({
          phoneNumber: phone
        })
        // app.wxGetRequest("/sharpproject/addWarnUserOfApplet", {
        //   "type": type,
        //   "userId": app.globalData.userInfo["user_id"],
        //   "monitorId": monitorId,
        //   "address": this.data.address,
        //   "tude": this.data.tude
        // }).then(result => {
        //   if (result.data.code == 1) {
        //     Toast.show({
        //       type: 'success',
        //       message: "上报成功!",
        //     });
        //   } else {
        //     Toast.show({
        //       type: 'close',
        //       message: "上报失败!",
        //     });
        //   }
        // });
      }
    })
  },
  /**
   * 播放视频
   */
  toplayMonitor: function(e) {
    this.setData({
      currentMonitorSrc: e.target.dataset.monitorSrc,
      currentMonitorId: e.target.dataset.monitorId
    }, function() {
      let livePlayerContext = wx.createLivePlayerContext("monitor");
      livePlayerContext.play();
      wx.hideLoading();
    });
  },
  playMonitor: function(e) {
    let id = e.target.dataset.monitorId;
    wx.showLoading({
      title: '加载中...',
    })
    app.wxGetRequest("/rtspToRtmp", {
      id: id
    }).then(result => {
      wx.showLoading({
        title: '加载中...',
      })
      if (result.data.code == 1) {
        this.toplayMonitor(e);
      }else{
        wx.hideLoading();
      }
    });
  },
  changeFullScreen: function(e) {
    let livePlayerContext = wx.createLivePlayerContext("monitor");
    if (this.data.isFullScreen) {
      livePlayerContext.exitFullScreen();
      this.setData({
        "isFullScreen": false
      })
    } else {
      livePlayerContext.requestFullScreen({
        "direction": 90
      });
      this.setData({
        "isFullScreen": true
      })
    }
  },
  /** 
   * 根据微信定位坐标，通过服务器查询上报地 祥址 
   */
  getCurrentLocationByServer: function() {
    wx.getLocation({
      success: res => {
        this.setData({
          tude: res.longitude + "," + res.latitude,
        });
        app.wxGetRequest("/applet/selectArea", {
          latitude: res.latitude,
          longitude: res.longitude
        }).then(result => {
          this.setData({
            address: result.data.address,
          });
        });
      },
      fail: function() {
        Toast.show({
          type: 'close',
          message: "坐标获取失败，请检查手机定位或小程序位置权限!",
        });
        return false;
      },
    })
  },
  bindstatechange: function(e) {
    if (e.detail.code == 2004) {
      wx.hideLoading();
    }
    if (e.detail.code == -2302) {
      wx.hideLoading();
      wx.showModal({
        title: "温馨提示",
        content: "获取数据失败!",
        showCancel: false,
        success: function(res) {
          if (res.confirm) {

          }
        }
      });
    }
  }
})